package com.aisile.manager.controller;

import com.aisile.entry.PageResult;
import com.aisile.entry.Result;
import com.aisile.pojo.TbBrand;
import com.aisile.service.BrandService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {
    @Reference
    BrandService brandService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<TbBrand> findAll(){
        return brandService.findAll();
    }

    @RequestMapping("findPage")
    @ResponseBody
    public PageResult findPage(
            @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
            @RequestParam(defaultValue = "10",value = "pageSize") Integer pageSize,@RequestBody TbBrand tbBrand){
        return brandService.findPage(tbBrand,pageNum,pageSize);
    }

    /**
     * 增加
     * @param brand
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Result add(@RequestBody TbBrand brand){
        try {
            brandService.add(brand);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public Result update(@RequestBody TbBrand brand){
        try {
            brandService.update(brand);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }
    /**
     * 获取实体
     * @param id
     * @return
     */
    @RequestMapping("findOne")
    @ResponseBody
    public TbBrand findOne(Long id){
        return brandService.findOne(id);
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Long [] ids){
        try {
            brandService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }
}
